package demo;

import jdk.nashorn.internal.objects.annotations.Where;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserMapper {
    public int insert(User user){
        try(Connection connection = DBUtil.getConnection();) {
            String sql = "INSERT INTO user(username,password,nickname,age) VALUES (?,?,?,?);";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUsername());
            preparedStatement.setString(2,user.getPassword());
            preparedStatement.setString(3,user.getNickname());
            preparedStatement.setInt(4,user.getAge());
            int d = preparedStatement.executeUpdate();
            return 1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public int select(UserLoginDTO user){
        try(Connection connection = DBUtil.getConnection();) {
            String sql = "SELECT id,username,password,nickname,age FROM user WHERE username=? AND password=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUsername());
            preparedStatement.setString(2,user.getPassword());
            int d = preparedStatement.executeUpdate();
            return d;
        } catch (SQLException e){
            e.printStackTrace();
        }
        return 0;
    }
    public User selectByUsername(String username){
        try(Connection connection = DBUtil.getConnection();) {
            String sql = "SELECT id,username,password,nickname,age FROM user WHERE username=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,username);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                User user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setNickname(resultSet.getString("nickname"));
                user.setAge(resultSet.getInt("age"));
                return user;
            }

        } catch (SQLException e){
            e.printStackTrace();
        }
        return null;
    }
    public int update(UserUpdate user){
        try(Connection connection = DBUtil.getConnection();) {
            String sql = "UPDATE user SET password=?,nickname=?,age=? WHERE username=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getPassword());
            preparedStatement.setString(2,user.getNickname());
            preparedStatement.setInt(3,user.getAge());
            preparedStatement.setString(4,user.getUsername());
            int d = preparedStatement.executeUpdate();
            return d;
        }catch (SQLException e){
            e.printStackTrace();
        }
        return 0;
    }

    public List<User> selectAll(){
        List<User> list = new ArrayList<>();
        try(Connection connection = DBUtil.getConnection();) {
            String sql = "SELECT id,username,password,nickname,age FROM user";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                User user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("username"));
                user.setNickname(resultSet.getString("nickname"));
                user.setPassword(resultSet.getString("password"));
                user.setAge(resultSet.getInt("age"));
                list.add(user);
            }
        }catch (SQLException e){
            e.printStackTrace();
            return null;
        }
        return list;
    }

}
